package com.ziyueyuwen.xiniao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suyeer.basic.util.EncryptUtil;
import com.ziyueyuwen.xiniao.bean.ZyRole;
import com.ziyueyuwen.xiniao.bean.ZyUser;
import com.ziyueyuwen.xiniao.bean.vo.ZyClassVO;
import com.ziyueyuwen.xiniao.bean.vo.ZyUserVO;
import com.ziyueyuwen.xiniao.bean.page.PageData;
import com.ziyueyuwen.xiniao.dao.ZyClassInfoMapper;
import com.ziyueyuwen.xiniao.dao.ZyUserMapper;
import com.ziyueyuwen.xiniao.enums.ClassUserTypeEnum;
import com.ziyueyuwen.xiniao.enums.StudentQueryEnum;
import com.ziyueyuwen.xiniao.service.RoleService;
import com.ziyueyuwen.xiniao.service.UserService;
import com.ziyueyuwen.xiniao.util.EEOUtil;
import com.ziyueyuwen.xiniao.util.PageDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wancheng  on 2018/12/17.
 */
@Service
public class UserServiceImpl implements UserService {

    private final String DEFAULT_PASSWORD = EncryptUtil.md5("123456");

    @Autowired
    ZyUserMapper zyUserMapper;
    @Autowired
    ZyClassInfoMapper zyClassInfoMapper;
    @Autowired
    RoleService roleService;

    @Override
    public ZyUserVO getUserMessage(String userAccount) {
        return zyUserMapper.getUserMessage(userAccount);
    }

    @Override
    public List<ZyUser> getAllStudent() {
        ZyUser param = new ZyUser();
        param.setIfSysUser(false);
        return zyUserMapper.select(param);
    }

    @Override
    public ZyUser getByAccount(String account) {

        return zyUserMapper.getByUserAccount(account);
    }

    @Override
    public PageData<ZyUserVO> getAllStudent(StudentQueryEnum queryType, String queryValue,Boolean ifBindWx, Integer page, Integer rows) {
        PageHelper.startPage(page,rows);
        List<ZyUserVO> students = zyUserMapper.selectStudentByQueryType(queryType,queryValue,ifBindWx);
        PageInfo<ZyUserVO> pageInfo = new PageInfo<>(students);
        PageData pageData = PageDataUtil.getPageData(pageInfo);
        List<ZyUserVO> aos = pageData.getData();
        aos.forEach(
                ao->{
                    List<ZyClassVO> classAOs = zyClassInfoMapper.getClassAdapterByStudentAccount(ao.getUserAccount());
                    ao.setZyClass(classAOs);
                }
        );
        pageData.setData(aos);
        return pageData;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZyUser saveStudent(ZyUser student) throws Exception {
        ZyUser ifExist = zyUserMapper.getByUserAccount(student.getUserAccount());
        if (ifExist != null && !ifExist.getIfDelete()){
            throw new Exception("手机号码已注册");
        }
        if (ifExist != null){
            ifExist.setUserName(student.getUserName());
            ifExist.setIfDelete(false);
            zyUserMapper.updateByPrimaryKeySelective(ifExist);
        }else {
            student.setUserPassword(DEFAULT_PASSWORD);
            String uid = EEOUtil.register(student.getUserAccount(),student.getUserName());
            student.setuId(uid);
            zyUserMapper.insertSelective(student);
        }
        //同步eeo
        EEOUtil.addSchoolStudent(student.getUserAccount(),student.getUserName());
        return student;
    }

    @Override
    public ZyUser saveTeacher(ZyUser teacher) throws Exception {
        ZyUser teacherExist = zyUserMapper.getByUserAccount(teacher.getUserAccount());
        if (teacherExist != null){
            teacherExist.setUserName(teacher.getUserName());
            teacherExist.setIfDelete(false);
            zyUserMapper.updateByPrimaryKeySelective(teacherExist);
        }else {
            teacher.setUserPassword(DEFAULT_PASSWORD);
            zyUserMapper.insertSelective(teacher);
        }
        //同步eeo
        EEOUtil.registerTeacher(teacher.getUserName(),teacher.getUserAccount());
        return teacher;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStudentName(ZyUser student) throws Exception {
        zyUserMapper.updateByPrimaryKeySelective(student);
        //student = zyUserMapper.selectByPrimaryKey(student.getId());
        //EEOUtil.editUserInfo(student.getUserAccount(),student.getUserName());
    }

    @Override
    public List<ZyUser> getStudents() {
        return zyUserMapper.getUserList(null);
    }

    @Override
    public List<ZyUser> getTeachers() {
        return zyUserMapper.getUserList(true);
    }

    @Override
    public List<ZyUser> getStudents(Integer classId) {
        return zyUserMapper.getClassUser(classId, ClassUserTypeEnum.STUDENT);
    }

    @Override
    public List<ZyUser> getStudentsByLessonId(Integer lessonId) {
        return zyUserMapper.getUserByLessonId(lessonId,ClassUserTypeEnum.STUDENT);
    }

    @Override
    public List<ZyUser> getTransferStudent(Integer lessonId) {
        return zyUserMapper.getTransferStudentsByLessonId(lessonId);
    }

    @Override
    public ZyUser getUserByDingTalkId(String dingTalkId) {
        return zyUserMapper.getByDingTalkId(dingTalkId);
    }

    @Override
    public PageData<ZyUserVO> getSysUser(String query, Integer page, Integer rows) {
        PageHelper.startPage(page,rows);
        PageHelper.orderBy(" create_time asc ");
        List<ZyUserVO> users = zyUserMapper.getUserByUserAccountOrUserName(query,true);
        PageInfo<ZyUserVO> pageInfo = new PageInfo<>(users);
        PageData pageData = PageDataUtil.getPageData(pageInfo);
        List<ZyUserVO> aos = pageData.getData();
        aos.forEach(
                ao->{
                    List<ZyRole> roles = roleService.getUerRolesByAccount(ao.getUserAccount());
                    ao.setRoles(roles);
                    String roleNames = roles.stream().map(ZyRole::getRoleName).collect(Collectors.joining("、"));
                    ao.setRoleNames(roleNames);
                }
        );
        pageData.setData(aos);
        return pageData;
    }

    @Override
    public ZyUser getByWxUserId(String wxUserId) {
        return zyUserMapper.getByWxUserId(wxUserId);
    }

    @Override
    public List<ZyUser> getStudentByAccountOrName(String query) {
        List<ZyUser> users = zyUserMapper.selectUserByNameLike(query);
        return users;
    }

    @Override
    public ZyUser getUserByUid(String uId) {
        ZyUser param = new ZyUser();
        param.setuId(uId);
        return  zyUserMapper.selectOne(param);
    }
}
